package com.example.service.impl;

import com.example.entity.User;
import com.example.entity.UserExample;
import com.example.exception.BusinessException;
import com.example.exception.BusinessExceptionCode;
import com.example.mapper.UserMapper;
import com.example.req.ResetUserPasswordRep;
import com.example.req.UserLoginRep;
import com.example.req.UserQueryReq;
import com.example.req.UserSaveRep;
import com.example.resp.UserLoginResp;
import com.example.service.UserService;
import com.example.utils.CopyUtil;
import com.example.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    SnowFlake snowFlake;

    @Override
    public PageInfo<User> findUser(final UserQueryReq req) {

        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if(!ObjectUtils.isEmpty(req.getName())){
            criteria.andNameLike("%"+req.getName()+"%");
        }
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<User> userList = userMapper.selectByExample(userExample);
        System.out.println(userList.toString());
        PageInfo<User> userPageInfo = new PageInfo<>(userList);
        return userPageInfo;
    }

    @Override
    public void saveUser(final UserSaveRep req) {
        User user = CopyUtil.copy(req, User.class);

        if(ObjectUtils.isEmpty(user.getId())){
            User userDB = selectByLoginNameEqualTo(req.getLoginName());
            if(ObjectUtils.isEmpty(userDB)){
                user.setId(snowFlake.nextId());
                userMapper.insert(user);
            }else {
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            }
        }else{
            user.setLoginName(null);
            user.setPassword(null);
            userMapper.updateByPrimaryKeySelective(user);
        }
    }

    public User selectByLoginNameEqualTo(String loginName){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andLoginNameEqualTo(loginName);
        List<User> userList = userMapper.selectByExample(userExample);

        if(CollectionUtils.isEmpty(userList)){
            return null;
        }else {
            return userList.get(0);
        }
    }

    @Override
    public void deleteUser(final Long id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void resetUserPassword(final ResetUserPasswordRep req) {
        User user = CopyUtil.copy(req, User.class);
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public UserLoginResp userLogin(final UserLoginRep req) {
        User userDB = selectByLoginNameEqualTo(req.getLoginName());
        if(!ObjectUtils.isEmpty(userDB)){

            if(userDB.getPassword().equals(req.getPassword())){
                UserLoginResp userLoginResp = CopyUtil.copy(userDB, UserLoginResp.class);
                return userLoginResp;
            }else {
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_ERROR);
            }

        }else {
            throw new BusinessException(BusinessExceptionCode.USER_LOGIN_ERROR);
        }
    }
}
